<?php

class Profile extends Controller {
	
	function Profile()
	{
		//Constructor  
		parent::Controller();
		
		//Check Login
		loginRequired('login');
		
		$this->load->model('usermodel');
		$this->load->model('friendsmodel');
		$this->load->model('wallmodel');
		$this->load->model('uploadmodel');
		$this->load->model('photomodel');
		$this->load->model('blogmodel');
		$this->load->library('form_validation');
	}

	function index()
	{
		$data['titlebar'] = "Panoramap | Profile";
		$data['sessUserId'] = $this->session->userdata('userId');
		$data['friends'] = $this->usermodel->getFriends($this->session->userdata('userId'));
		$data['username'] = $this->usermodel->getName($this->session->userdata('userId'));//ถ้าระบบมัน getname ไม่ได้ จะถูก redirect ไปยัง error page ทันที
		$data['avatar'] = $this->usermodel->getAvatar($this->session->userdata('userId'));
		$data['about'] = $this->usermodel->getAbout($this->session->userdata('userId'));
		$data['lat'] = $this->usermodel->getLat($this->session->userdata('userId'));
		$data['lng'] = $this->usermodel->getLng($this->session->userdata('userId'));
		$data['notifications'] = $this->friendsmodel->getRequestCount();
		$data['album'] = $this->photomodel->getAlbum($this->session->userdata('userId'), 'datetime desc', '0', '20');

		$data['wall'] = $this->wallmodel->getWall($this->session->userdata('userId'), 'wall_date desc', '0', '9');
		$this->load->view('profile_view', $data);
	}
	
	/// test
	function myWall()
	{
		$this->load->model('wallmodel');
		$data['wall'] = $this->wallmodel->get_wall($this->session->userdata('userId'));
		$data['titlebar'] = "Panoramap.in.th | test";
		$this->load->view('testwall_view', $data);
	}
	/// end
	
	
	function myWallPost()
	{
		$this->load->model('wallmodel');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('wallpost', 'Wall Post', 'trim|required|xss_clean');

		if($this->form_validation->run() == FALSE)
		{
			$this->session->set_flashdata('error',  'กรุณาใส่ข้อความที่ต้องการโพส.');
			redirect('profile');	
		}
		else
		{
			$userid = $this->session->userdata('userId');
			$this->wallmodel->wallPost($userid, $userid, $this->input->post('wallpost'), 1);
			redirect('profile');
		}
	}
	
	function subWallPost()
	{
		$this->load->model('wallmodel');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('comment', 'Comment Post', 'trim|required|xss_clean');

		if($this->form_validation->run() == FALSE)
		{
			$this->session->set_flashdata('error',  'กรุณาใส่ข้อความที่ต้องการโพสก่อน.');
			redirect('profile');	
		}
		else
		{
			$ref_wall = $this->uri->segment(3);
			$userid = $this->session->userdata('userId');
			$this->wallmodel->subWallPost($userid, $ref_wall, $this->input->post('comment'));
			redirect('profile');
		}
	}
	
	function deleteWall()
	{
	$this->load->model('wallmodel');
		$wallId = $this->uri->segment(3);
		$this->wallmodel->deleteWall($wallId);
		redirect('profile');
	}
	
	function deleteSubWall()
	{
		
		$this->load->model('wallmodel');
		$subWallId = $this->uri->segment(3);
		$this->wallmodel->deleteSubWall($subWallId);
		redirect('profile');
	}
	
	function edit()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
		
		if($this->form_validation->run() == FALSE)
		{
		$data['lat'] = $this->usermodel->getLat($this->session->userdata('userId'));
		$data['lng'] = $this->usermodel->getLng($this->session->userdata('userId'));
		$data['email'] = $this->usermodel->getEmail($this->session->userdata('userId'));
		$data['about'] = $this->usermodel->getAbout($this->session->userdata('userId'));
		$data['username'] = $this->usermodel->getName($this->session->userdata('userId'));
		$data['titlebar'] = 'Panoramap.in.th | แก้ไขข้อมูลส่วนตัว';
		$this->load->view('edit_view', $data);
		}
		else
		{
			$username = $this->input->post('username');
			$email = $this->input->post('email');
			$about = $this->input->post('about');
			$this->db->where('user_id', $this->session->userdata('userId'));
			$this->db->update('users', array('username' => $username, 'email' => $email, 'about' => $about));
			redirect('profile');
		}
	}
	
	function mymap()
	{
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('latitude', 'Latitude', 'trim|required|xss_clean');
		$this->form_validation->set_rules('longitude', 'Longitude', 'trim|required|xss_clean');
		
		if($this->form_validation->run() == FALSE)
		{
			$data['lat'] = $this->usermodel->getLat($this->session->userdata('userId'));
			$data['lng'] = $this->usermodel->getLng($this->session->userdata('userId'));
			$data['titlebar'] = 'Panoramap.in.th | ระบุตำแหน่งที่ตั้ง';
			$this->load->view('getmap_view', $data);
		}
		else
		{
			$lat = $this->input->post('latitude');
			$lng = $this->input->post('longitude');
			$this->db->where('user_id', $this->session->userdata('userId'));
			$this->db->update('users', array('latitude' => $lat, 'longitude' => $lng));
			redirect('profile');
		}
	}
	
	function uploadAvatar()
	{	
		
		$this->load->model('avatarmodel');
		$this->load->model('usermodel');
		
		if($this->input->post('upload'))
		{
			$this->avatarmodel->upload_avatar($this->session->userdata('userId'));
			$data['titlebar'] = "Panoramap.in.th | เปลี่ยนภาพประจำตัว";
			$data['avatar'] = $this->usermodel->getAvatar($this->session->userdata('userId'));
			$this->load->view('avatar_view', $data);
		}
		else
		{
			$data['titlebar'] = "Panoramap.in.th | เปลี่ยนภาพประจำตัว";
			$data['avatar'] = $this->usermodel->getAvatar($this->session->userdata('userId'));
			$this->load->view('avatar_view', $data);
		}
		
		
	}
	
	function changePassword()
	{

		$this->load->library('form_validation');
		$this->form_validation->set_rules('currentPwd', 'Current Password', 'trim|required|xss_clean|md5');
		$this->form_validation->set_rules('newPwd', 'New Password', 'trim|required|min_length[4]|max_length[12]|matches[newPwd2]|xss_clean|md5');
		$this->form_validation->set_rules('newPwd2', 'Password Confirmation', 'trim|required|min_length[4]|max_length[12]|xss_clean');

		if($this->form_validation->run() == FALSE)
		{
			$data['titlebar'] = "Panoramap.in.th | เปลี่ยนแปลงรหัสผ่าน";
			$this->load->view('changepwd_view', $data);
		}
		else
		{
			$userId = $this->session->userdata('userId');
			$currentPwd = $this->input->post('currentPwd');
			$newPwd = $this->input->post('newPwd');
			$this->load->model('usermodel');
			$this->usermodel->changePwd($userId, $currentPwd, $newPwd);
		}
	}
	
	function notifications()
	{
		
		$this->load->model('friendsmodel');
		$data['notifications'] = $this->friendsmodel->getRequest($this->session->userdata('userId'));
		$data['titlebar'] = "Panoramap.in.th | Notifications";
		
		$this->load->view('notifications_view', $data);
	}
	
	function accept()
	{
		
		$requestId = $this->uri->segment(3);
		$requestUsername = $this->uri->segment(4);
		if($requestId == '' or $requestUsername == '')
		{
			redirect('profile');
		}
		else
		{
		$this->load->model('friendsmodel');
		$this->load->model('wallmodel');
		
		//$normal = 1;
		$action = 2;
		$am = $this->usermodel->getName($this->session->userdata('userId'));
		$data = "<img src='".base_url()."application/images/follow.gif' /><i>" . $am . " ได้ยอมรับการติดตามของ " .$requestUsername."</i>";
		$this->wallmodel->wallPost($this->session->userdata('userId'), $this->session->userdata('userId'), $data, $action);
		
		$this->friendsmodel->acceptRequest($requestId);
		$this->session->set_flashdata('success',  'คุณได้ยอมรับการติดตามของ ' . $requestUsername .' แล้ว.');
		redirect('profile/notifications');
		}
	}
	
	function reject()
	{
		
		$requestId = $this->uri->segment(3);
		$requestUsername = $this->uri->segment(4);
		if($requestId == '' or $requestUsername == '')
		{
			redirect('profile');
		}
		else
		{
		$this->load->model('friendsmodel');
		$this->friendsmodel->rejectRequest($requestId);
		$this->session->set_flashdata('error',  'คุณปฏิเสธการติดตามของ ' . $requestUsername );
		redirect('profile/notifications');
		}
	}
	
	function blog()
	{
		$data['titlebar'] = "Panoramap.in.th | My Blog";
		$blogId = $this->uri->segment(3);
		$data['lastBlog'] = $this->blogmodel->getBlog($this->session->userdata('userId'), 10);
		$data['lastComment'] = $this->blogmodel->getLastComment($this->session->userdata('userId'), 10);
		$data['blog'] = $this->blogmodel->getBlog($this->session->userdata('userId'), 1, $blogId);
		$this->load->view('blog_view', $data);
	}
	
	function blog_create()
	{
		$this->form_validation->set_rules('blog_name', 'Blog Name', 'trim|required');
		$this->form_validation->set_rules('blog_content', 'Blog Content', 'trim|required');
		
		if($this->form_validation->run() == FALSE)
		{
			$data['titlebar'] = "Panoramap.in.th | สร้างบล็อกใหม่";
			$this->load->view('blog_create_view', $data);
		}
		else
		{
			$this->blogmodel->newEntry($this->session->userdata('userId'));
		}
		
	}
	
	function upload()
	{
		
		$this->form_validation->set_rules('photo_name', 'photo name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('photo_status', 'privilege', 'trim|required|xss_clean');
		$this->form_validation->set_rules('type', 'Categories', 'trim|required|xss_clean');
		
		if($this->form_validation->run() == FALSE)
		{
			$data['titlebar'] = "Panoramap.in.th | อัพโหลดภาพถ่ายของคุณ";
			$this->load->view('upload_view', $data);
		}
		else
		{
			$userId = $this->session->userdata('userId');
			$this->uploadmodel->upload_photo($userId);
		}
		

	}
	
	function album()
	{
		
		$data['album'] = $this->photomodel->getAlbum($this->session->userdata('userId'));
		$data['titlebar'] = "Panoramap.in.th | อัลบั้มภาพถ่ายของคุณ";
		$this->load->view('album_view', $data);
	}
	
	function photo()
	{
		/*
		$data['photo'] = $this->photomodel->getPhoto($this->uri->segment(3));
		$data['titlebar'] = "Panoramap.in.th | My Photo";
		$this->load->view('photo_view', $data);
		*/

		$photo = $this->photomodel->getUserPhoto($this->uri->segment(3));		
		$data['titlebar'] = $photo[0]['location'] . "&nbsp;" . $photo[0]['detail'];
		$data['photoUser'] = $photo[0]['user_id'];
		$data['photoDatetime'] = $photo[0]['datetime'];
		$data['photoCaption'] = $photo[0]['caption'];
		$data['photoLat'] = $photo[0]['latitude'];
		$data['photoLng'] = $photo[0]['longitude'];
		$data['photoLoc'] = $photo[0]['location'];
		$data['photoDetail'] = $photo[0]['detail'];
		$this->load->view('user_photo_view', $data);
	}
	
	function delphoto($photoId)
	{
		$photoId = $this->uri->segment(3);
		$this->photomodel->delete($photoId);
	}
	
	function addPoint()
	{
		
		$this->form_validation->set_rules('latitude', 'latitude', 'trim|required|xss_clean');
		$this->form_validation->set_rules('longitude', 'longitude', 'trim|required|xss_clean');
		
		if($this->form_validation->run() == FALSE)
		{
		
			$data['titlebar'] = "Panoramap.in.th | Thickbox Page";
			$this->load->view('addpoint_view', $data);
		}
		else
		{
			$lat = $this->input->post('latitude');
			$lng = $this->input->post('longitude');
			$photoId = $this->input->post('photoId');
			$this->photomodel->addPoint($lat, $lng, $photoId);
		}

	}
	
	function friends()
	{
		
		$data['titlebar'] = "Panoramap.in.th | ผู้ที่ติดตามคุณทั้งหมด";
		$data['friends'] = $this->usermodel->getFriends($this->session->userdata('userId'));
		$this->load->view('friends_view', $data);
	}


	function error()
	{
		$data['titlebar'] = "Panoramap.in.th - อุ๊ปส์! คุณมีปัญหาแล้วละ.";
		$this->load->view('user_error_view', $data);
	}

}